/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TCleanshop;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderCarCleanRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.OrderCarCleanResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderCarCleanResponseHandler;
import com.gmail.blackdog1987.carcare.widget.ImgViewerWindow;
public class OrderCleanActivity extends BaseActivity implements OnResponseListener, OnClickListener {
	private Context mContext;
	
	CubeImageView head;
	TextView title;
	TextView location;
	TextView content;
	TextView price;
	EditText license;
	View pay;
	TCleanshop shop;
	CubeImageView img1;
	CubeImageView img2;
	CubeImageView img3;
	CubeImageView img4;
	CubeImageView img5;
	
	OrderCarCleanRequestBuilder req = new OrderCarCleanRequestBuilder();
	OrderCarCleanResponseHandler handler = new OrderCarCleanResponseHandler();
	
	private Intent imgViewIntent;
	
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_clean);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		head = (CubeImageView) findViewById(R.id.order_clean_head);
		title = (TextView) findViewById(R.id.order_clean_title);
		location = (TextView) findViewById(R.id.order_clean_location_str);
		price = (TextView) findViewById(R.id.order_clean_cost);
		pay = findViewById(R.id.order_clean_pay);
		content = (TextView) findViewById(R.id.order_clean_content);
		img1 = (CubeImageView) findViewById(R.id.clean_img1);
		img2 = (CubeImageView) findViewById(R.id.clean_img2);
		img3 = (CubeImageView) findViewById(R.id.clean_img3);
		img4 = (CubeImageView) findViewById(R.id.clean_img4);
		img5 = (CubeImageView) findViewById(R.id.clean_img5);
		license = (EditText) findViewById(R.id.order_clean_license);
	}

	public void reg() {
		shop = (TCleanshop) getIntent().getSerializableExtra("data");
		pay.setOnClickListener(this);
		handler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.order_clean_location).setOnClickListener(this);
		findViewById(R.id.order_clean_call).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		imgViewIntent = new Intent(OrderCleanActivity.this, ImgViewerWindow.class);
		shop = (TCleanshop) getIntent().getSerializableExtra("data");
		ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
		if(shop.headurl!=null&&shop.headurl.length()!=0){
			App.loadImg(head, imageLoader, shop.headurl);
		}
		title.setText(shop.getTitle());
		location.setText(shop.getLocation());
		price.setText(String.valueOf(shop.getPrice()));
		content.setText(shop.getContent()==null?"":shop.getContent());
		
		// set views data
		if(shop.img1!=null&&shop.img1.length()!=0){
			App.loadImg(img1, imageLoader, shop.img1);
			img1.setOnClickListener(this);
		}
		if(shop.img2!=null&&shop.img2.length()!=0){
			App.loadImg(img2, imageLoader, shop.img2);
			img2.setOnClickListener(this);
		}
		if(shop.img3!=null&&shop.img3.length()!=0){
			App.loadImg(img3, imageLoader, shop.img3);
			img3.setOnClickListener(this);
		}
		if(shop.img4!=null&&shop.img4.length()!=0){
			App.loadImg(img4, imageLoader, shop.img4);
			img4.setOnClickListener(this);
		}
		if(shop.img5!=null&&shop.img5.length()!=0){
			App.loadImg(img5, imageLoader, shop.img5);
			img5.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.order_clean_location:
			Intent intent = new Intent(OrderCleanActivity.this, MapGuidActivity.class);
			intent.putExtra("lat", shop.lat);
			intent.putExtra("lng", shop.lon);
			startActivity(intent);
			break;
		case R.id.order_clean_call:
			if(shop.phone==null||shop.phone.length()==0){
				Toast.makeText(mContext, "尚未录入商家电话", Toast.LENGTH_LONG).show();
				return;
			}
			Intent callintent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+shop.phone));  
            startActivity(callintent);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.order_clean_pay:
			if(!App.validUser(this))
				return;
			if(license.getText().toString().trim().length()==0){
				Toast.makeText(mContext, "请填入您的车牌号", Toast.LENGTH_LONG).show();
				return;
			}
			req.setLicense(license.getText().toString().trim()).setShopid(String.valueOf(shop.getId())).postJsonRequest(mContext, handler);
			break;
		case R.id.clean_img1:
			imgViewIntent.putExtra("url", shop.img1);
			startActivity(imgViewIntent);
			break;
		case R.id.clean_img2:
			imgViewIntent.putExtra("url", shop.img2);
			startActivity(imgViewIntent);
			break;
		case R.id.clean_img3:
			imgViewIntent.putExtra("url", shop.img3);
			startActivity(imgViewIntent);
			break;
		case R.id.clean_img4:
			imgViewIntent.putExtra("url", shop.img4);
			startActivity(imgViewIntent);
			break;
		case R.id.clean_img5:
			imgViewIntent.putExtra("url", shop.img5);
			startActivity(imgViewIntent);
			break;
		default:
			break;
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==Activity.RESULT_OK){
			switch(requestCode){
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
//		Toast.makeText(mContext, "", Toast.LENGTH_LONG).show();
//		finish();
		TProduct product = new TProduct();
		product.body = "洗车";
		product.price = Double.valueOf(shop.getPrice());
		product.subject = shop.getTitle() + " 洗车 ";
		product.tableName = "t_order_clean";
		product.tableId = ((OrderCarCleanResponse)classz.getResponse()).order.getId();
		product.userid = new UserDao().getUser().id;
		Intent i = new Intent(OrderCleanActivity.this, PayActivity.class);
		i.putExtra("data", product);
		i.putExtra("url", shop.headurl);
		startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败", Toast.LENGTH_LONG).show();
	}
}
